package Student;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86156
 * Date: 2023-04-10
 * Time: 9:40
 */
public class SIMS {

    private static final int ADD_STUDENT = 1;
    private static final int DELETE_STUDENT = 2;
    private static final int REVISE_STUDENT = 3;
    private static final int SELECT_STUDENT = 4;
    public static void main(String[] args) {

        ArrayList<Student> list = new ArrayList<Student>();
        while (true) {
            System.out.print(
                    "-------------欢迎来到zuiacsn学生管理系统----------------\n"+
                    "1：添加学生\n"+
                    "2：删除学生\n"+
                    "3：修改学生\n"+
                    "4：查询学生\n"+
                    "5：退出\n"+
                    "请输入您的选择:");

            Scanner sc = new Scanner(System.in);
            int id= 0;
            switch(sc.nextInt()){
                case ADD_STUDENT :
                    addStudent(list);
                    break;
                case DELETE_STUDENT :
                    System.out.println("请输入要删除学生的学号:");
                    id = sc.nextInt();
                    deleteStudent(list,id);
                    break;
                case REVISE_STUDENT :
                    System.out.println("请输入要修改学生的学号");
                    id = sc.nextInt();
                    reviseStudent(list,id);
                    break;
                case SELECT_STUDENT:
                    selectStudent(list);
                default :
                    break;
            }
            print(list);
            if(id<0||id>4){
                break;
            }
        }
    }


    public static void print(ArrayList<Student> list){
        System.out.println("id\taddress\tage\tname");
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i).getId()+" "
                    +list.get(i).getAddress()+" "+
                    list.get(i).getAge()+" "+
                    list.get(i).getName());
        }
    }


    public static void selectStudent(ArrayList<Student> list){
        if(list.size() == 0){
            System.out.println("当前无信息,请添加后再查询");
        }
        // 表头信息
        System.out.println("id\taddress\tage\tname");
        for (int i = 0; i < list.size(); i++) {
           Student stu = list.get(i);
                System.out.println(list.get(i).getId()+"\t"
                        +list.get(i).getAddress()+"\t"+
                        list.get(i).getAge()+"\t"+
                        list.get(i).getName());
        }
        System.out.println("此学生不存在,将返回主菜单");
    }

    public static ArrayList<Student> reviseStudent(ArrayList<Student> list,int id){
        Student stu = new Student();

        for (int i = 0; i < list.size(); i++) {
            stu = list.get(i);

            if(stu.getId() == id){

                Scanner sc = new Scanner(System.in);
                Student stud = new Student();
                System.out.println("请输入学生Id");
                stud.setId(sc.nextInt());

                System.out.println("请输入学生姓名");
                stud.setName(sc.next());

                System.out.println("请输入学生年龄");
                stud.setAge(sc.nextInt());

                System.out.println("请输入学生地址");
                stud.setAddress(sc.next());
                list.set(i,stud);
                return list;
            }
        }
        System.out.println("修改的id不存在,将返回主菜单");
        return list;
    }




    public static ArrayList<Student> deleteStudent(ArrayList<Student> list,int id){
        Student stu = new Student();
        for (int i = 0; i < list.size(); i++) {
            stu = list.get(i);
            if(stu.getId() == id){
                list.remove(i);
                return list;
            }
        }
            System.out.println("id不存在,将返回主菜单");
        return list;
    }



    public static ArrayList<Student> addStudent(ArrayList<Student> list){
        Scanner sc = new Scanner(System.in);
        Student stu = new Student();
        System.out.println("请输入学生Id");
        stu.setId(sc.nextInt());

        System.out.println("请输入学生姓名");
        stu.setName(sc.next());

        System.out.println("请输入学生年龄");
        stu.setAge(sc.nextInt());

        System.out.println("请输入学生地址");
        stu.setAddress(sc.next());

        for (int i = 0; i < list.size(); i++) {
            Student stud = list.get(i);
            if(stu.getId() == stud.getId()){
                return list;
            }
        }
        list.add(stu);
        return list;
    }
}


/*    addStudent(list);
    addStudent(list);
    addStudent(list);
    print(list);

    deleteStudent(list,2);
    print(list);
    reviseStudent(list,1);
    print(list);

    selectStudent(list,1);*/
